package com.jg.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jg.comm.responce.PageResult;
import com.jg.comm.util.FileUploadUtil;
import com.jg.model.dto.ProductQuery;
import com.jg.model.entity.Products;
import com.jg.mapper.ProductsMapper;
import com.jg.model.vo.ProductVo;
import com.jg.service.ProductsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * <p>
 * 供应商商品信息表 服务实现类
 * </p>
 *
 * @author 86130
 * @since 2025-09-19 11:21
 */
@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;
    @Autowired
    private FileUploadUtil fileUploadUtil;


    @Override
    public PageResult<ProductVo> getProductPage(ProductQuery productQuery) {
        Page<ProductVo> page = new Page<>(productQuery.getPageNum(), productQuery.getPageSize());
        Page<ProductVo> productPage = productsMapper.getProductPage(page, productQuery);
        return PageResult.of(productPage);
    }

    @Override
    public void editProduct(Products products, MultipartFile imgUrl) {
        String imgUrlPath = null;
        if (imgUrl != null) {
            try {
                imgUrlPath = fileUploadUtil.uploadImage(imgUrl);
            }catch (IOException e){
                throw new RuntimeException(e);
            }
        }
        products.setImageUrl(imgUrlPath);
        this.saveOrUpdate(products);
    }
}
